// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// c [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/mixin/check_plan_mixin.dart';
import 'package:finance_app/src/provider/dashboard/credit_debit/views/credit_note_provider.dart';
import 'package:finance_app/src/provider/dashboard/credit_debit/views/debit_note_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/widgets/bottom_sheet/delete_info_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class CreditDebitProvider extends BaseProvider with CheckPlanMixin {
  List<String> tabList = [
    S.current.credit_note,
    S.current.debit_note,
  ];

  bool buttonBarIsUp = true;

  List<String> menuList = ['Delete'];

  @override
  Future get dataFuture => null;

  int contactId;

  @override
  List<BaseProvider> registerChildProviders() {
    return [
      CreditNoteProvider(),
      DebitNoteProvider(),
    ];
  }

  void handleScrollUpdate(ScrollController controller) {
    if (controller.position.userScrollDirection == ScrollDirection.reverse &&
        buttonBarIsUp == true) {
      //向下滑动
      buttonBarIsUp = false;
      update();
    }
    if (controller.position.userScrollDirection == ScrollDirection.forward &&
        buttonBarIsUp == false) {
      buttonBarIsUp = true;
      update();
    }
  }

  void onNavAdd() {
    if (isPlanExpired()) return;
    obtainContext?.call((context) async {
      if (tabController != null) {
        currentIndex =
            (tabController.index + tabController.offset).round().clamp(0, 100);
      }
      switch (currentIndex) {
        case 0:
          await Routes.navigateTo(RouterGenProviders.newCreditNotePage);
          getChildProvider<CreditNoteProvider>()?.onRefresh();

          break;
        case 1:
          await Routes.navigateTo(RouterGenProviders.newDebitNotePage);
          getChildProvider<DebitNoteProvider>()?.onRefresh();
          break;
      }
    });
  }

  void onSelectMenu(value) async {
    int index = menuList.indexOf(value);
    if (index == 0) {
      switch (currentIndex) {
        case 0:
          final result =
              await Routes.navigateTo(RouterGenProviders.deleteCreditNotePage);
          if (result != null && result is Map && result['delete'] != null) {
            getChildProvider<CreditNoteProvider>()
                .refreshController
                .requestRefresh();
          }
          break;
        case 1:
          final result =
              await Routes.navigateTo(RouterGenProviders.deleteDebitNotePage);
          if (result != null && result is Map && result['delete'] != null) {
            getChildProvider<DebitNoteProvider>()
                .refreshController
                .requestRefresh();
          }
          break;
      }
    }
  }

  int currentIndex = 0;

  TabController tabController;

  void onChangeTab(TabController tabController) {
    currentIndex = (tabController.index + tabController.offset).round();
  }
}
